﻿using MediatR;
using RedDragon.Common.Domain.warehouse;
using RedDragon.Common.Infrastructure.Interface;
using T.Common.Domain.Common;

namespace RedDragon.RBAC.API.Application.Common.WarehouseW
{
    public class WarehouseSeachHandler : IRequestHandler<WarehouseSeachCommon, Result>
    {
        public IBaseRepository<Warehouse> WarehouseRepository;

        public WarehouseSeachHandler(IBaseRepository<Warehouse> warehouseRepository)
        {
            WarehouseRepository = warehouseRepository;
        }

        public async Task<Result> Handle(WarehouseSeachCommon request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = 0, Msg = "" };
            try
            {
                var list = await WarehouseRepository.GetAll();
                if (request.Name !=null)
                {
                    list = list.Where(x => x.WarehouseName.Contains(request.Name));
                }
                if (request.Status!=-1)
                {
                    list = list.Where(x => Convert.ToInt32( x.WarehouseState)==request.Status);
                }
                
                result.Code = 0;
                result.Msg = "";
                result.Data = list.ToList();
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
